package gentle.controller;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.BaseController;
import gentle.entity.PerformanceInfo;
import gentle.entity.SearchPerformanceInfo;
import gentle.factory.PageFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import gentle.service.IPerformanceService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 费用明细
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/performance")
public class PerformanceController extends BaseController {
    @Autowired
    private IPerformanceService performanceService;

    /**
     * 业绩查询列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object selectAchievementList(SearchPerformanceInfo info, HttpServletRequest request) {
        if(info.getAbbreviation() == null || info.getAbbreviation() =="" || info.getAbbreviation() =="null"){
            return null;
        }else{
            Page<PerformanceInfo> page = new PageFactory<PerformanceInfo>().defaultPage();
            try {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<PerformanceInfo> list = performanceService.selectPageListQueryPages(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = performanceService.selectPageListQueryPages(page, info);
                }
                page.setRecords(list);
                page.setTotal(performanceService.getQueryPagesCount(page, info));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    /**
     * 费用明细 统计查询.
     */
    @RequestMapping(value = "/total", method = RequestMethod.GET)
    @ResponseBody
    public PerformanceInfo total(String reach, String startRechargeTime, String endRechargeTime, String oneChannel1, String twoChannel1,
                                 String thrChannel1, String oneMedium1, String twoMedium1,String thrMedium1, String scarchiving1, String tcName1, String outpatientName,
                                 String isDoName1, String treatmentUnit1, String customerStatus1){
        return performanceService.total(reach, startRechargeTime, endRechargeTime, oneChannel1, twoChannel1, thrChannel1, oneMedium1, twoMedium1,
                thrMedium1, scarchiving1, tcName1, outpatientName, isDoName1, treatmentUnit1, customerStatus1);
    }

    @RequestMapping(value = "/performanceTableExport")
    public void performanceTableExport(@Valid SearchPerformanceInfo searchInfo, HttpServletResponse response) {
        performanceService.performanceTableExport(searchInfo,response);
    }

}